$(document).ready(function () {

    //获取当前时间
    var date = new Date();
    var now_year = date.getFullYear();
    var now_month = date.getMonth()+1;
    var now_day = date.getDate();
    var today = now_year + "-" + now_month + "-" + now_day;
    $("#today_time").html(now_year + "年" + now_month + "月" + now_day+"日");

    //初始化界面
    flushRoomsStock(today);

    //十五天后
    var fifteen_day = new Date(date);
    fifteen_day.setDate(date.getDate() + 15);
    var new_year = fifteen_day.getFullYear();
    var new_month = fifteen_day.getMonth()+1;
    var new_day = fifteen_day.getDate();
    var after_fifteen = new_year + "-" + new_month + "-" + new_day;

    //选择时间
    $('#changeDayBtn').click(function(){ //假设 test1 是一个按钮
        laydate.render({
            elem: '#today',
            value: new Date(),
            showBottom: false,
            theme: 'default',
            min: today,
            max: after_fifteen,
            show: true,
            //这里代表的意思是：点击 test1 所在元素阻止关闭事件冒泡。如果不设定，则无法弹出控件
            closeStop: '#changeDayBtn',
            done: function(value){
                var list = value.split("-");
                var time = list[0] + "年" + list[1] + "月" + list[2] + "日";
                $("#today_time").html(time);
                flushRoomsStock(value);
            }
        });
    });

    //预定时间选择
    laydate.render({
        elem: '#book_time',
        range: true,
        min: today,
        max: after_fifteen,
    });

});

function flushRoomsStock(day) {
    $.ajax({
        url: "back_home/rooms_stock",
        type: "POST",
        dataType: "json",
        data:{
            day: day
        },
        success : function (data) {
            var content = '';
            content += '<div class="row bg-white has-shadow ">';
            for (var i = 0; i < data.length; i++) {
                var color = '';
                var random = parseInt(Math.random()*5);
                switch (random) {
                    case 1:
                        color = 'red';
                        break;
                    case 2:
                        color = 'green';
                        break;
                    case 3:
                        color = 'yellow';
                        break;
                    case 4:
                        color = 'orange';
                        break;
                    case 0:
                        color = 'blue';
                        break;
                }
                var po = data[i];
                // onclick="check_order('+po.room_type_id+')"
                content += '<div class="col-xl-3 col-sm-6 dropdown">'+
                    '           <div class="btn" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\n' +
                    '                <div class="item d-flex align-items-center">\n' +
                    '                   <div class="icon bg-'+color+'"><i class="fa fa-hotel"></i></div>\n' +
                    '                    <div class="title"><span>'+po.room_type_name+'</span>\n' +
                    '                      <div class="progress">\n' +
                    '                        <div role="progressbar" style="width: 25%; height: 4px;" aria-valuenow="20" aria-valuemin="0" aria-valuemax="'+po.room_count+'" class="progress-bar bg-gray"></div>\n' +
                    '                      </div>\n' +
                    '                    </div>\n' +
                    '                   <div class="number"><strong>'+po.room_count+'</strong></div>\n' +
                    '                 </div>\n' +
                    '             </div>'+
                    '           <div class="dropdown-menu">\n' +
                    '               <a class="dropdown-item" href="javascript:void(0)" onclick="check_order('+po.room_type_id+',\''+po.room_type_name+'\','+po.room_count+')">预定</a>\n' +
                    '               <a class="dropdown-item" href="javascript:void(0)">开房</a>\n' +
                    '           </div>'+
                    '        </div>';
                if (i == 3) {
                    content += '</div>\n' +
                        '              <br>\n' +
                        '              <div class="row bg-white has-shadow">';
                }
            }
            content += ' </div>';
            $("#rooms_stock").html(content);
        }
    });
}


function check_order(id,name,count) {
    if (count == 0) {
        alert("没有剩余房间!");
    }else {
        $("#book_type").prop("placeholder",name);
        $("#room_type_id").val(id);
        $("#order_info").modal('show');
    }

}